//提交订阅消息示例

const pushReservationTmplIds = 'YXd57l651Ck4m1S2FvN6t-4LiBs7G9qHzwWyOWJ1r1M';

export default class Sign{
    config = {
        navigationBarBackgroundColor: '#fff',
        navigationBarTitleText: '赢积分',
    };
    submitClock() {
       console.log('预定成功')
    }
    setClock(e) {
        let that = this;

        console.log(wx.requestSubscribeMessage);

        if (wx.requestSubscribeMessage) {

            wx.requestSubscribeMessage({
                tmplIds: [pushReservationTmplIds],
                success(res) {
                    if (res[pushReservationTmplIds] === 'accept') {
                        that.submitClock();
                    } else if (res[pushReservationTmplIds] === 'reject') {
                        // 用户历史操作有设置了拒绝 or 关闭了订阅消息的主（总）开关，导致无法推送
                        // console.log(res, '0 拒绝 or 关闭了订阅消息的主（总）开关---');
                        that.guideOpenSubscribeMessage();
                    } else {
                        wx.showToast({
                            title: '授权订阅消息有误',
                            icon: 'none'
                        });
                    }
                },
                fail(res) {
                    // 20004:用户关闭了主开关，无法进行订阅,引导开启
                    if (res.errCode == 20004) {
                        // console.log(res, 'fail:用户关闭了主开关，无法进行订阅,引导开启---');
                        that.guideOpenSubscribeMessage();
                    }
                }
            });
        } else {
            wx.showToast({
                title: '请更新您微信版本，来获取订阅消息功能',
                icon: 'none'
            });
        }
    }
    guidSubscribeMessageAuthAfter() {
        //引导用户 开启订阅消息 之后，「openSetting」 接口暂时不会返回，用户手动设置后的状态，所以采用「getSetting」接口重新进行查询
        wx.getSetting({
            withSubscriptions: true,
            success: res => {
                let {
                    authSetting = {},
                    subscriptionsSetting: { mainSwitch = false, itemSettings = {} } = {}
                } = res;

                if (
                    (authSetting['scope.subscribeMessage'] || mainSwitch) &&
                    itemSettings[pushReservationTmplIds] === 'accept'
                ) {
                    this.submitClock();
                    // console.log('用户手动开启同意了，订阅消息');
                } else {
                    this.$invoke('Toast', '__warning__', [
                        `您没有同意授权订阅消息，预约领取失败`
                    ]);
                }
            }
        });
    }
    guideOpenSubscribeMessage() {
        //引导用户，手动引导用户去设置页开启，
        this.$invoke('Modals', '__modalConfirm__', [
            '检测到您没有开启订阅消息的权限，是否去设置？',
            'openSetting',
            //用户点击了确定按钮，进入设置页的回调
            res => {
                console.log('openSetting的回调数据：', res);
                this.guidSubscribeMessageAuthAfter();
            },
            //用户点击了取消按钮
            () => {
                // console.log("取消了")
                this.$invoke('Toast', '__warning__', [
                    `您已拒绝订阅消息授权，无法预约领取`
                ]);
            }
        ]);
    }
}

